package com.surfilter.admin.project.system.controller;

import com.surfilter.admin.common.constant.UserConstants;
import com.surfilter.admin.common.utils.SecurityUtils;
import com.surfilter.admin.common.utils.poi.ExcelUtil;
import com.surfilter.admin.framework.aspectj.lang.annotation.Log;
import com.surfilter.admin.framework.aspectj.lang.enums.BusinessType;
import com.surfilter.admin.framework.web.controller.BaseController;
import com.surfilter.admin.framework.web.domain.AjaxResult;
import com.surfilter.admin.framework.web.page.TableDataInfo;
import com.surfilter.admin.project.system.domain.SysConfig;
import com.surfilter.admin.project.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置 信息操作处理
 * 
 *
 */
@Api(tags = "参数配置")
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController
{
    @Autowired
    private ISysConfigService configService;

    /**
     * 获取参数配置列表
     */
    @ApiOperation(value = "查询列表", notes = "查询列表", response = TableDataInfo.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "config", value = "系统", required = true, paramType = "query",
            dataType = "config")})
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysConfig config)
    {
        startPage();
        List<SysConfig> list = configService.selectConfigList(config);
        return getDataTable(list);
    }

    @ApiOperation(value = "导出参数列表", notes = "导出参数列表", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "config", value = "系统配置参数", required = true, paramType = "query",
            dataType = "SysConfig")})
    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping("/export")
    public AjaxResult export(SysConfig config)
    {
        List<SysConfig> list = configService.selectConfigList(config);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        return util.exportExcel(list, "参数数据");
    }

    /**
     * 根据参数编号获取详细信息
     */
    @ApiOperation(value = "根据参数编号获取详细信息", notes = "根据参数编号获取详细信息", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "configId", value = "配置参数Id", required = true, paramType = "query",
            dataType = "Long")})
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return AjaxResult.success(configService.selectConfigById(configId));
    }

    /**
     * 根据参数键名查询参数值
     */
    @ApiOperation(value = "根据参数键名查询参数值", notes = "根据参数键名查询参数值", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "configKey", value = "配置参数Key", required = true, paramType = "query",
            dataType = "String")})
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 新增参数配置
     */
    @ApiOperation(value = "新增参数配置", notes = "新增参数配置", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "config", value = "系统参数配置", required = true, paramType = "query",
            dataType = "SysConfig")})
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(configService.insertConfig(config));
    }

    /**
     * 修改参数配置
     */
    @ApiOperation(value = "修改参数配置", notes = "修改参数配置", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "config", value = "系统参数配置", required = true, paramType = "query",
            dataType = "SysConfig")})
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfig config)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(config)))
        {
            return AjaxResult.error("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateConfig(config));
    }

    /**
     * 删除参数配置
     */
    @ApiOperation(value = "删除参数配置", notes = "删除参数配置", response = AjaxResult.class)
    @ApiImplicitParams({@ApiImplicitParam(name = "configIds", value = "系统参数配置Id集合", required = true, paramType = "query",
            dataType = "Long[]")})
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(configService.deleteConfigByIds(configIds));
    }
}
